#include <iostream>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <set>
#include <vector>
#include <cstdio>
#include <cmath>
#define LL long long
#define nextLine() {int c = 0; while ((c = getchar()) != EOF && c != 10);}
#define sqr(a) ((a) * (a))
#define fill(a, c) memset(a, c, sizeof a)
#define ldb long double
#define sgn(a) ((a) > eps ? 1 : -1)
const ldb eps = 1e-9;
const ldb pi = fabs(atan2(0.0, -1.0));
const int INF = 1 << 28;
const LL LINF = 1ll << 50;
const ldb LDINF = 1e70;
const ldb cos3 = cos(pi / 3);
const ldb sin3 = sin(pi / 3);
using namespace std;
#define y1 y6

int n, x1, y1, x2, y2;
int x[4], y[4];



void Load()
{
	cin >> n >> x1 >> y1 >> x2 >> y2;
}


vector <pair <int, int> > fst, sec;

void Solve()
{
	

	x[0] = 0;
	y[0] = 0;
	x[1] = n;
	y[1] = 0;
	x[2] = n;
	y[2] = n;
	x[3] = 0;
	y[3] = n;
	int i, j;
	for (i = 0; i < 4; i++)
	{
		if (x[i] == x[(i + 1) % 4] && x[i] == x1)
		{
			fst.push_back(make_pair(x[i], y[i]));
			fst.push_back(make_pair(x[(i + 1) % 4], y[(i + 1) % 4]));
		}
		if (x[i] == x[(i + 1) % 4] && x[i] == x2)
		{
			sec.push_back(make_pair(x[i], y[i]));
			sec.push_back(make_pair(x[(i + 1) % 4], y[(i + 1) % 4]));
		}
		if (y[i] == y[(i + 1) % 4] && y[i] == y1)
		{
			fst.push_back(make_pair(x[i], y[i]));
			fst.push_back(make_pair(x[(i + 1) % 4], y[(i + 1) % 4]));
		}
		if (y[i] == y[(i + 1) % 4] && y[i] == y2)
		{
			sec.push_back(make_pair(x[i], y[i]));
			sec.push_back(make_pair(x[(i + 1) % 4], y[(i + 1) % 4]));
		}
	}
	sort(sec.begin(), sec.end());
	sort(fst.begin(), fst.end());
	int res = INF;
	if (x1 == x2 && (x1 == 0 || x1 == n))
	{
		res = abs(y1 - y2);
	}
	if (y1 == y2 && (y1 == 0 || y1 == n))
	{
		res = abs(x1 - x2);
	}

	for (i = 0; i < fst.size(); i++)
	{
		for (j = 0; j < sec.size(); j++)
		{
			if (fst[i].first != sec[j].first && fst[i].second != sec[j].second) continue;
			res = min(abs(x1 - fst[i].first) + abs(y1 - fst[i].second) + abs(x2 - sec[j].first) + abs(y2 - sec[j].second)
			+ abs(fst[i].first - sec[j].first) + abs(fst[i].second - sec[j].second), res);
		}
	}
	cout << res;
}

int main()
{
	#ifndef ONLINE_JUDGE
	freopen("in", "rt", stdin);
	freopen("out", "wt", stdout);
	#endif
	Load();
	Solve();
	return 0;
}